package com.ly.dingtalk.service.login.impl;

import org.jetbrains.annotations.NotNull;

import com.aliyun.dingtalkoauth2_1_0.Client;
import com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponseBody;
import com.ly.dingtalk.common.exception.DingApiException;
import com.ly.dingtalk.component.application.IDingMiniH5;
import com.ly.dingtalk.service.AbstractDingNewApi;
import com.ly.dingtalk.service.login.IDingLoginHandler;
import com.ly.dingtalk.service.login.IDingLoginHandler.GrantType;

import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wangzhen 2023/3/23
 */
@Slf4j
public class DingLoginHandler extends AbstractDingNewApi implements IDingLoginHandler {
    public DingLoginHandler(IDingMiniH5 app) {
        super(app);
    }

    /**
     * @see com.ly.dingtalk.service.user.impl.DingUserNewHandler.findUserByLoginAuthCode(String)
     * @Description: 可以调用findUserByLoginAuthCode 获取用户信息
     * @see com.ly.dingtalk.service.login.IDingLoginHandler#getUserToken(java.lang.String, com.ly.dingtalk.service.login.IDingLoginHandler.GrantType)
     * @date 2023-12-27 09:45:33
     */
    @SneakyThrows(Exception.class)
    @Override
    public GetUserTokenResponseBody getUserToken(@NotNull String authCode, @NonNull GrantType grantType) {
        GetUserTokenRequest request = new GetUserTokenRequest()
                .setClientId(app.getAppKey())
                .setClientSecret(app.getAppSecret())
                .setCode(authCode)
                .setGrantType(grantType.name());

        final GetUserTokenResponse response = execute(
                Client.class,
                client -> {
                    try {
                        return client.getUserToken(request);
                    } catch (Exception e) {
                        log.error("获取用户token失败", e);
                        throw new DingApiException("获取用户token失败");
                    }
                },
                () -> "获取用户token"
        );
        return response.getBody();
    }

    @Override
    public CreateJsapiTicketResponse createJsapiTicketWithOptions() {
        return execute(com.aliyun.dingtalkoauth2_1_0.Client.class, client -> {
            try {
                com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders createJsapiTicketHeaders = new com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders();
                createJsapiTicketHeaders.xAcsDingtalkAccessToken = accessToken();
                return client.createJsapiTicketWithOptions(createJsapiTicketHeaders, new com.aliyun.teautil.models.RuntimeOptions());
            } catch (Exception e) {
                log.error("createJsapiTicketWithOptions失败", e);
                throw new DingApiException(e.getMessage());
            }
        }, ()->"获取jsapiTicket");
    }
}
